package com.springboot.test.common.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ConvertUtils {
    public static <T> T convert(Object source, Class<T> targetClass) {
        if (source == null) {
            return null;
        } else {
            try {
                T target = targetClass.newInstance();
                BeanUtils.copyProperties(source, target);
                return target;
            } catch (IllegalAccessException | InstantiationException var3) {
                var3.printStackTrace();
                return null;
            }
        }
    }

    public static <T> List<T> convert(List source, Class<T> clazz) {
        ArrayList<T> result = new ArrayList();
        if (CollectionUtils.isEmpty(source)) {
            return result;
        } else {
            Iterator var3 = source.iterator();

            while (var3.hasNext()) {
                Object s = var3.next();
                result.add(convert(s, clazz));
            }

            return result;
        }
    }
}
